package top.yokey.learn2.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager.widget.ViewPager;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import top.yokey.learn2.MyApp;
import top.yokey.learn2.MyService;
import top.yokey.learn2.R;
import top.yokey.learn2.view.FixedViewPager;
import top.yokey.learn2.view.FragmentAdapter;

public class MainActivity extends AppCompatActivity {

    private FixedViewPager mainViewPager;
    private AppCompatTextView[] textViews = new AppCompatTextView[5];
    private Drawable[] normal = new Drawable[5];
    private Drawable[] press = new Drawable[5];

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent1 = new Intent();
        intent1.setAction("MyBroadcastReceiver.Custom");
        intent1.putExtra("info", "123456789");
        intent1.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        sendBroadcast(intent1);

        mainViewPager = findViewById(R.id.mainViewPager);
        textViews[0] = findViewById(R.id.homeTextView);
        textViews[1] = findViewById(R.id.shopTextView);
        textViews[2] = findViewById(R.id.addTextView);
        textViews[3] = findViewById(R.id.messageTextView);
        textViews[4] = findViewById(R.id.mineTextView);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ShopFragment());
        fragments.add(new HomeFragment());
        fragments.add(new HomeFragment());
        fragments.add(new HomeFragment());
        normal[0] = getDrawable(R.drawable.ic_navigation_home);
        normal[1] = getDrawable(R.drawable.ic_navigation_shop);
        normal[2] = getDrawable(R.drawable.ic_navigation_add);
        normal[3] = getDrawable(R.drawable.ic_navigation_message);
        normal[4] = getDrawable(R.drawable.ic_navigation_mine);
        press[0] = getDrawable(R.drawable.ic_navigation_home_press);
        press[1] = getDrawable(R.drawable.ic_navigation_shop_press);
        press[2] = getDrawable(R.drawable.ic_navigation_add_press);
        press[3] = getDrawable(R.drawable.ic_navigation_message_press);
        press[4] = getDrawable(R.drawable.ic_navigation_mine_press);

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        mainViewPager.setAdapter(adapter);
        mainViewPager.setScanScroll(true);

        mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < textViews.length; i++) {
                    textViews[i].setTextColor(Color.GRAY);
                    textViews[i].setCompoundDrawablesWithIntrinsicBounds(null, normal[i], null, null);
                }
                textViews[position].setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.navigation_press));
                textViews[position].setCompoundDrawablesWithIntrinsicBounds(null, press[position], null, null);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < textViews.length; i++) {
            final int pos = i;
            textViews[pos].setOnClickListener(view -> {
                mainViewPager.setCurrentItem(pos);
                if (pos == 0) {
                    //发送消息到事件总线
                    LiveEventBus.get("test").post("这是事件总线发出的数据");
                    //写入数据到SP
                    intent = new Intent(MainActivity.this, MyService.class);
                    startService(intent);
                }
                if (pos == 1) {
                    stopService(intent);
                }
            });
        }

        LiveEventBus.get("position", int.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                mainViewPager.setCurrentItem(integer);
            }
        });

        String key = MyApp.get().getSharedPreferences().getString("key", "");
        if (key.isEmpty()) {
            Toast.makeText(this, "令牌为空，请重新登陆", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
        }

        OkGo.<String>get("https://demo.shopwt.com/api/mobile/index.php?w=member_favorites&t=favorites_list&key=" + key + "&curpage=1&page=10").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if (jsonObject.getInt("code") == 400) {
                        jsonObject = jsonObject.getJSONObject("datas");
                        Toast.makeText(MainActivity.this, jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                        MyApp.get().getEditor().putString("key", "").apply();
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        finish();
                        return;
                    }
                    jsonObject = jsonObject.getJSONObject("datas");
                    Toast.makeText(MainActivity.this, jsonObject.toString(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
